package net;
import game.Livello;

import java.awt.MediaTracker;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

import sprite.Giocatore;
import sprite.Mattone;
import sprite.Proiettile;



public class ServerMulti extends Thread {
    //private Socket socket = null;
    String invia;
    PrintWriter out;
    BufferedReader in;
    boolean fine = false;
    Socket s;   
    ObjectOutputStream obj;
    String user = "";
    
    public ServerMulti(PrintWriter out,ObjectOutputStream obj, BufferedReader in,Socket s) {
        this.out = out;
        this.in = in;
        this.s = s;
        this.obj = obj;
        System.out.println("Nuova connesione");
       
        try {
			user = in.readLine();
		} catch (IOException e) {
			System.out.println("eccezione nella lettura dell'user");
		}
		System.out.println("User ricevuto: " + user);
		Server.user.add(user);
        Server.stato.addPlayer();
        Server.stato.getPlayerAt(Server.clientList.indexOf(out)).setAttivaThread(true);
        Server.stato.getPlayerAt(Server.clientList.indexOf(out)).setUser(user);
        inviaStato();
        out.println("ciao dal server");
        //AVVISO TUTTI I CLIENT DEL NUOVO COLLEGAMENTO
    	for(PrintWriter o:Server.clientList) {
    		if(!o.equals(out))
    			o.println("#n"+user);
    	}  
    	this.start();
        
    }
    
    public void inviaStato() {    	
    	try {
			obj.writeObject(Server.stato);
			obj.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
   
    public void run() {
        try {                   
            while (!fine) {   
                try {
                    String inputLine = in.readLine();
                    processaInput(inputLine);
                } catch (Exception e) {         

                	fine = true;

                	
                }     
            }
            Server.stato.getPlayers().remove(Server.clientList.indexOf(out));
            //AVVISO TUTTI I CLIENT CHE IL GIOCATORE E' USCITO
            int index = Server.clientList.indexOf(out);
        	for(PrintWriter o:Server.clientList) {
        		o.println("#u"+index);
        	}              
        	Server.clientList.remove(out);
        	Server.user.remove(user);
            out.close();
            in.close();
            s.close();
            //socket.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void processaInput(String input) {
    	if(input.startsWith("*")) {
        	for(PrintWriter o:Server.clientList) {
        		o.println("<"+user+">"+input.substring(1));
        	}      			
    	}
    	else if(input.equals("#u")) {
    		fine = true;
    	}
    	else if(input.equals("#a")) {
    		int index = Server.clientList.indexOf(out);
    		Server.stato.getPlayerAt(index).possoAndareAvanti();
        	for(PrintWriter o:Server.clientList) {
        		o.println("#a"+index);
        	}     		
    		
    	}
    	else if(input.equals("#d")) {
    		int index = Server.clientList.indexOf(out);
    		Server.stato.getPlayerAt(index).possoAndareIndietro();
        	for(PrintWriter o:Server.clientList) {
        		o.println("#d"+index);
        	}     		
    		
    	}
    	else if(input.equals("#s")) {
    		int index = Server.clientList.indexOf(out);
    		Server.stato.getPlayerAt(index).possoSaltare();
        	for(PrintWriter o:Server.clientList) {
        		o.println("#s"+index);
        	}     			
    	}
    	else if(input.equals("#f")) {
    		int index = Server.clientList.indexOf(out);
    		Server.stato.getPlayerAt(index).setCorri(false);
        	for(PrintWriter o:Server.clientList) {
        		o.println("#f"+index);
        	}     			
    	}
    	else if(input.equals("#p")) {
    		int index = Server.clientList.indexOf(out);
    		Server.stato.getPlayerAt(index).shoot();
        	for(PrintWriter o:Server.clientList) {
        		o.println("#p"+index);
        	}     			
    	}
    	else if(input.equals("#r")) {
    		int index = Server.clientList.indexOf(out);
    		Server.stato.getPlayerAt(index).setSpara(false);
        	for(PrintWriter o:Server.clientList) {
        		o.println("#r"+index);
        	}     			
    	}
    	else if(input.startsWith("#c")) {
    		int index = Server.clientList.indexOf(out);
        	for(PrintWriter o:Server.clientList) {
        		o.println(input);
        	}
			String[] tmp = input.split("#c");
			String[] tmp2 = tmp[1].split("%");
			Giocatore g = Server.stato.getPlayerAt(Integer.parseInt(tmp2[1]));
			g.setPunteggio(g.getPunteggio()+1);
			System.out.println("PUNTEGGIO:" + g.getPunteggio());
    	}    
    	else {
        	for(PrintWriter o:Server.clientList) {
        		o.println(input);
        	}  		
    	}
    }
}
